#!/usr/bin/env python
# -*- coding: utf_8 -*-

#    guteninja: a frontend for various video-games
#    Copyright (C) 2007  Tor-björn Claesson

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

try:
    import pygame
    from pygame.locals import *
    import pygame.font
    has_pygame = True
except:
    has_pygame = False
from config import *
import sys
class game_information(pygame.sprite.Sprite):
    #Ritar upp ett spel med tillhörande information.
    
    def __init__(self, game_data):
        #Superklassens konstruktor
        pygame.sprite.Sprite.__init__(self)
        try:
            picture = pygame.image.load(game_data.path + game_data.image)
        except:
            print "Kunde inte läsa in: ", game_data.image
        
        self.surface = pygame.Surface((320,300))
        self.surface.convert()
        self.surface.fill((0,0,0))
        self.surface.blit(picture, (160 - picture.get_rect().width/2.0, 150 - picture.get_rect().height/2.0))
        
class game_name(pygame.sprite.Sprite):
    def __init__(self, game_name):
        #Call the base class contructor
        pygame.sprite.Sprite.__init__(self)
        game_name.convert()
        self.surface = game_name
        self.rect = game_name.get_rect()
        
class interface_object(pygame.sprite.Sprite):
    def __init__(self, path, name):
        pygame.sprite.Sprite.__init__(self)
        try:
            self.surface = pygame.image.load(path)
        except:
            "Kunde inte läsa in: ", path
        
        self.name = name
        self.surface.convert()
        self.rect = self.surface.get_rect()

class interface():
    def _get_graphical(self):
        return self._graphical
    def _set_graphical(self, graphical):
        self._graphical = graphical
    graphical = property(_get_graphical, _set_graphical)
    
    def _get_games(self):
        return self._games
    def _set_games(self, games):
        self._games = games
    games = property(_get_games, _set_games)
    
    def __init__(self, graphical=True, game_list=None):
        if graphical and has_pygame:
            self.graphical = True
        else:
            self.graphical = False
        
        if game_list != None:
            self.games = []
            self.games = game_list

    def run(self):
        if len(self.games) == 1:
            return self.games[0]
        
        if self.graphical:
            return self._run_graphical()
        else:
            return self._run_text()
    
    def _run_text(self):
        print "Hej, välj ett spel!:"
        i = 0
        while i < len(self.games):
            print str(i + 1) + ". " + self.games[i].gamename
            i = i + 1
                
        i = int(raw_input("\n - "))
        return self.games[i - 1]
    def draw_screen(self):
        #Draw the background
        background = pygame.Surface(self.screen.get_size())
        background.convert()
        background.fill((0,0,0))
        self.screen.blit(background, (0, 0))
        #Draw static interface elements:
        for i in self.interface_elements:
            if (i.name == "square"):
                self.screen.blit(i.surface, (27, 300))
            elif (i.name == "h_line"):
                self.screen.blit(i.surface, (279, 316))
        
        #Draw the game information
        self.screen.blit(self.game_list[self.selected_game].surface, (300, 10))
        
        #Draw the game_names
        i = 0
        while i < len(self.game_names):
            self.screen.blit(self.game_names[i].surface, ((308.0-self.game_names[i].rect.width)/2, 307 + (self.selected_game-i)*30))
            i = i + 1
        

    def load_interface_elements(self):
        elements = pygame.sprite.Group()
        square = interface_object(DATADIR + "images/square.png", "square")
        
        h_line = interface_object(DATADIR + "images/h_line.png", "h_line")
        
        elements.add((square, h_line))
        return elements
        
    def move_up(self):
        pass
    
    def move_down(self):
        pass
    
    def _run_graphical(self):
        #Starta det grafiska gränssnittet        
        pygame.init()
        pygame.font.init()
        window = pygame.display.set_mode((640, 480), FULLSCREEN, 16)
        pygame.display.set_caption("Pekomaten")
        self.screen = pygame.display.get_surface()
        #Load the font
        fontrenderer = pygame.font.Font(None, 32)
        fontrenderer.set_bold(True)        
        
        #Läs in alla spelbilder och sätt ihop sprites med dessa samt lite info om spelen.
        self.game_list = []
        self.game_names = []
        for i in self.games:
            self.game_list.append(game_information(i))
             
        for i in self.games:
            self.game_names.append(game_name(fontrenderer.render(i.gamename, True, (255,0,0))))
        self.selected_game = 0
        
        self.interface_elements = self.load_interface_elements()
        
        #Draw the initial screen
        self.draw_screen()
        pygame.display.flip()
        running = 1
        pygame.event.set_blocked((MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN))
        clock = pygame.time.Clock()
        while running:
            #Wait for an event
            j = pygame.event.get()
            pygame.event.clear()
            for i in j:
                if i.type == QUIT:
                    running = 0
                    pygame.display.quit()
                    pygame.quit()
                    sys.exit(0)
                if i.type == KEYDOWN:
                    if i.key == K_DOWN:
                        if self.selected_game > 0:
                            self.move_down()
                            self.selected_game = self.selected_game -1
                            self.draw_screen()
                            pygame.display.flip()
                    if i.key == K_UP:
                        if (self.selected_game + 1) < len(self.games):
                            self.move_up()
                            self.selected_game = self.selected_game + 1
                            self.draw_screen()
                            pygame.display.flip()
                    if (i.key == K_KP_ENTER) or (i.key == K_RETURN):
                        running = 0
            clock.tick(30)
            
            
        #When done, free stuff:
        pygame.display.quit()
        pygame.quit()
        self.game_list = None
        self.game_names = None
        self.interface_elements = None
        self._games = None
        self.screen = None
        #Return
        return self.games[self.selected_game]

